/*
 * Copyright (C) 2019 ByteDance Inc
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bytedance.scene.group;

import com.bytedance.scene.State;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;

/**
 * Created by JiangQi on 9/19/18.
 * Need to provide a set of LifeCycle
 */
public abstract class AsyncLayoutGroupScene extends UserVisibleHintGroupScene {
    private boolean mAsyncLayoutEnabled = false;
    private boolean mViewAsyncCreated = false;

    @Override
    public final ComponentContainer onCreateView(LayoutScatter inflater, ComponentContainer container, final Intent savedInstanceState) {
        if (this.mAsyncLayoutEnabled) {
            final StackLayout frameLayout = new StackLayout(requireSceneContext());
            SceneAsyncLayoutInflater asyncLayoutInflater = new SceneAsyncLayoutInflater(requireSceneContext());
            asyncLayoutInflater.inflate(getLayoutId(), container, (view, resid, parent) -> {
                State state = getState();
                if (state == State.NONE) {
                    return;
                }

                if (state.value >= State.VIEW_CREATED.value) {
                    StackLayout.LayoutConfig layoutConfig = new StackLayout.LayoutConfig(StackLayout.LayoutConfig.MATCH_PARENT,StackLayout.LayoutConfig.MATCH_PARENT);
                    frameLayout.addComponent(view,layoutConfig);
                    mViewAsyncCreated = true;
                    onAsyncViewCreated(getView(), savedInstanceState);
                }

                if (state.value >= State.ACTIVITY_CREATED.value) {
                    onAsyncActivityCreated(savedInstanceState);
                }

                if (state.value >= State.STARTED.value) {
                    onAsyncStart();
                }

                if (state.value == State.RESUMED.value) {
                    onAsyncResume();
                }
            });
            return frameLayout;
        } else {
            Component view = inflater.parse(getLayoutId(), container, false);
            if (!(view instanceof ComponentContainer)) {
                throw new IllegalArgumentException("AsyncLayoutGroupScene getLayoutId() view must be ViewGroup");
            }
            this.mViewAsyncCreated = true;
            return (ComponentContainer) view;
        }
    }

    protected abstract int getLayoutId();

    public boolean isViewAsyncCreated() {
        return this.mViewAsyncCreated;
    }

    public void setAsyncLayoutEnabled(boolean value) {
        this.mAsyncLayoutEnabled = value;
    }

    public boolean isAsyncLayoutEnabled() {
        return this.mAsyncLayoutEnabled;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            setAsyncLayoutEnabled(false);
        } else {
            setAsyncLayoutEnabled(true);
        }
    }

    @Override
    public final void onViewCreated(Component view, Intent savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (isViewAsyncCreated()) {
            onAsyncViewCreated(view, savedInstanceState);
        }
    }

    @Override
    public final void onActivityCreated(Intent savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (isViewAsyncCreated()) {
            onAsyncActivityCreated(savedInstanceState);
        }
    }

    @Override
    public final void onStart() {
        super.onStart();
        if (isViewAsyncCreated()) {
            onAsyncStart();
        }
    }

    @Override
    public final void onResume() {
        super.onResume();
        if (isViewAsyncCreated()) {
            onAsyncResume();
        }
    }

    @Override
    public final void onPause() {
        super.onPause();
        if (isViewAsyncCreated()) {
            onAsyncPause();
        }
    }

    @Override
    public final void onStop() {
        super.onStop();
        if (isViewAsyncCreated()) {
            onAsyncStop();
        }
    }

    public void onAsyncViewCreated(Component view, Intent savedInstanceState) {

    }

    public void onAsyncActivityCreated(Intent savedInstanceState) {

    }


    public void onAsyncStart() {

    }


    public void onAsyncResume() {

    }


    public void onAsyncPause() {

    }

    public void onAsyncStop() {

    }
}
